//
//  NotesDetailViewController.m
//  Funny
//
//  Created by dllo on 15/10/31.
//  Copyright © 2015年 King. All rights reserved.
//

#import "NotesDetailViewController.h"
#import "NotesDetailTableViewCell.h"

@interface NotesDetailViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *arr;
@property (nonatomic, strong)UIImageView *headIMG;
@property (nonatomic, strong)UIImageView *userImg;
@property (nonatomic, strong)UILabel *username;
@property (nonatomic, strong)UILabel *titlelable;

@end

@implementation NotesDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self handle];
    [self.view addSubview:self.tableView];
    UIButton *backClick = [UIButton buttonWithFrame:CGRectMake(0, 23, 60, 30) color:nil title:@" 返回" type:UIButtonTypeSystem target:nil action:@selector(backToPage) event:UIControlEventTouchUpInside cornerRadius:0];
    backClick.tintColor = [UIColor whiteColor];
    [backClick setImage:[UIImage imageNamed:@"btn_back@3x.png"] forState:UIControlStateNormal];
    UIImageView *backImg= [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 66)];
    
    backImg.image = [UIImage imageNamed:@"back1.png"];
    [self.view addSubview:backImg];
    [self.view addSubview:backClick];

}

- (void)handle
{
    NSString *URLStr = [NSString stringWithFormat:@"http://open.qyer.com/qyer/footprint/mguide_detail?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=10&id=%@&lat=38.88270678796906&lon=121.539278349696&page=1&track_app_channel=App Store&track_app_version=6.8&track_device_info=iPhone7,2&track_deviceid=357AAE6A-2231-4843-84E7-0ED164F35BDD&track_os=ios 9.0.2&v=1",self.ID];
    
    [HandleData handleDataWithUrl:URLStr completion:^(id data) {
        NSDictionary *dic1 = data;
        NSDictionary *dic2 = [dic1 objectForKey:@"data"];
        self.arr = [dic2 objectForKey:@"pois"];
    
        [self.headIMG sd_setImageWithURL:[NSURL URLWithString:[dic2 objectForKey:@"photo"]] placeholderImage:[UIImage imageNamed:@"image.png"]];
        [self.userImg sd_setImageWithURL:[NSURL URLWithString:[dic2 objectForKey:@"avatar"]]];
        self.username.text = [dic2 objectForKey:@"username"];
        self.titlelable.text = [dic2 objectForKey:@"title"];
        [self.tableView reloadData];
    }];
}
- (UIImageView *)headIMG
{
    if (!_headIMG) {
        _headIMG = [[UIImageView alloc]init];
        _headIMG.frame = CGRectMake(0, -380*H, self.view.bounds.size.width, 240*H);
        _headIMG.contentMode = UIViewContentModeScaleAspectFill;
        [self.tableView addSubview:_headIMG];
        
    }
    return  _headIMG;
}

- (void)backToPage
{
    [self.navigationController popViewControllerAnimated:NO];
}

- (UIImageView *)userImg
{
    if (!_userImg) {
        _userImg = [[UIImageView alloc]init];
        _userImg.backgroundColor = [UIColor grayColor];
        _userImg.layer.masksToBounds = YES;
        _userImg.layer.cornerRadius = 30;
        [self.tableView addSubview:_userImg];
        [_userImg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.tableView);
            make.centerY.equalTo(self.tableView.mas_top).with.offset(-140);
            make.size.mas_equalTo(CGSizeMake(60*W, 60*H));
        }];
    }
    return _userImg;
}

- (UILabel *)username
{
    if (!_username) {
        _username = [[UILabel alloc]init];
        _username.font = [UIFont systemFontOfSize:12];
        _username.textAlignment = NSTextAlignmentCenter;
        [self.tableView addSubview:_username];
        [_username mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.tableView);
            make.centerY.equalTo(self.userImg.mas_bottom).with.offset(10);
            make.size.mas_equalTo(CGSizeMake(150*W, 15*H));
        }];
    }
    return _username;
}

- (UILabel *)titlelable
{
    if (!_titlelable) {
        _titlelable = [[UILabel alloc]init];
        _titlelable.font = [UIFont systemFontOfSize:16];
        _titlelable.textAlignment = NSTextAlignmentCenter;
        [self.tableView addSubview:_titlelable];
        [_titlelable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.tableView);
            make.centerY.equalTo(self.username.mas_bottom).with.offset(40);
            make.size.mas_equalTo(CGSizeMake(300*W, 30*H));
        }];
    }
    return _titlelable;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat y = scrollView.contentOffset.y;
    if (y < - 360 * H) {
        CGRect frame = _headIMG.frame;
        frame.origin.y = y;
        frame.size.height = -y - 140 * H;
        _headIMG.frame = frame;
    }
}

- (NSMutableArray *)arr
{
    if (!_arr) {
        _arr = [[NSMutableArray alloc]init];
    }
    return _arr;
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
                _tableView.separatorStyle = NO;
        _tableView.showsVerticalScrollIndicator = NO;
        [_tableView registerClass:[NotesDetailTableViewCell class] forCellReuseIdentifier:@"foodlist"];
        _tableView.contentInset = UIEdgeInsetsMake(360*H, 0, 0, 0);
    }
    return _tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NotesDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"foodlist"];
    NSDictionary *dic = [self.arr objectAtIndex:indexPath.row];
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"photo"]] placeholderImage:[UIImage imageNamed:@"chartlet_image_traning.png"]];
    cell.TEXTLebel.text = [dic objectForKey:@"description"];
    cell.titleLabel.text = [dic objectForKey:@"firstname"];
    cell.scoreLabel.text = [dic objectForKey:@"beenstr"];
    //设置行间距 自适应高度
    [self setLineSpacing:12 label:cell.TEXTLebel];
    CGRect rect = [[dic objectForKey:@"description"] boundingRectWithSize:CGSizeMake(355*W, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:19]} context:nil];
    cell.TEXTLebel.frame = CGRectMake(10*W, 210*H, 355*W, rect.size.height);
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = [self.arr objectAtIndex:indexPath.row];
    CGRect rect = [[dic objectForKey:@"description"] boundingRectWithSize:CGSizeMake(355*W, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:19]} context:nil];
    return rect.size.height + 220 * H;
}

- (void)setLineSpacing:(CGFloat)spacing label:(UILabel *)label
{
    NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:label.text];
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:spacing];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [label.text length])];
    [label setAttributedText:attributedString];
    [label sizeToFit];
}

-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
